import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { availableParallelism } from 'node:os';
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },

  routeRules: {
    '/**': { ssr: true }
  },
  runtimeConfig: {
    // data: {
    //   // kv 绑定名称
    //   binding: 'my_kv'
    // },
    public: {
      DATABASE_URL: process.env.DATABASE_URL
    }
  },
  modules: ['@nuxtjs/robots', '@nuxtjs/sitemap'],

  // site: {
  //   // 站点地址 必须填写
  //   url: 'https://test.kxyr.top'
  // },
  robots: {
    blockNonSeoBots: true,
    blockAiBots: true
  },

  

  nitro: {
    sourceMap: false,
    baseURL: '/',
    experimental: {
      // 开启openapi
      openAPI: true
    },
    openAPI: {
      route: '/swagger/openapi.json',
      // 生产环境开启
      production: 'prerender', // 或 "prerender"
      ui: {
        scalar: false,
        swagger: {
          // swagger ui 路由 默认 /_swagger
          route: '/swagger'
        }
      }
    },
    hooks: {
      'build:before'(nitro) {
        // openapi 修改
        if (nitro.options.experimental?.openAPI) {
          const openApiJson = nitro.options.openAPI?.route || '/_openapi.json';

          const openapi =
            existsSync(join(nitro.options.output.publicDir, openApiJson)) &&
            JSON.parse(readFileSync(join(nitro.options.output.publicDir, openApiJson), 'utf-8'));

          if (openapi) {
            delete openapi.servers;
            writeFileSync(
              join(nitro.options.output.publicDir, openApiJson),
              JSON.stringify(openapi)
            );
          }
        }
      }
    },
    output: {
      dir: '.edgeone',
      publicDir: '.edgeone/assets',
      serverDir: '.edgeone/server-handler',
    }
  },
  vite: {
    css: {
      // 启用预处理器
      preprocessorMaxWorkers: availableParallelism()
    }
  }
});
